﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

using System.Windows;

namespace PoshBoard.VisualDesigner.Controls
{
   public  class PSScriptHelper
    {
        /// <summary>
        /// Build Powershell Script for a Parent component, and for its children
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="parentName"></param>
        /// <param name="Children"></param>
        /// <returns></returns>
        public static string BuildPSScript(IPBComponentBase parent, string parentName, List<IPBComponentBase> Children)
        {
            string script = Environment.NewLine + parent.PSName + "=" + parent.CommandBase + " " + parent.GetParameters();

            if (Children != null)
            {
                foreach (IPBComponentBase child in Children)
                {
                    child.BuildPSName(parentName);
                    script += Environment.NewLine;
                    script += child.GetConstructionScript();
                    script += Environment.NewLine;
                    script += "Add-PBChild -InputObject " + parent.PSName + " -Children " + child.PSName;
                }
            }
            return script;
        }


        /// <summary>
        /// Build common paramenters, depending on inheritance of the control
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static string GetCommonParameters(UIElement control)
        {
            string param = string.Empty;

            if (control is Control)
                param += GetControlParameters(control as Control);

            if (control is FrameworkElement)
                param += GetFrameworkElementParameters(control as FrameworkElement);

            return param;
        }

        public static string GetControlParameters(Control control)
        {
            string param = string.Empty;

            //param += "  -FontSize " + control.FontSize;

            return param;
        }

        public static string GetFrameworkElementParameters(FrameworkElement control)
        {

            string param = string.Empty;

            if (!(double.IsNaN(control.Width)) && (control.Width != double.PositiveInfinity))
                param += " -Width " + control.Width.ToString();

            if (!(double.IsNaN(control.MaxWidth)) && (control.MaxWidth != double.PositiveInfinity))
                param += " -MaxWidth " + control.MaxWidth.ToString();

            if (!(double.IsNaN(control.MinWidth)) && (control.MinWidth != double.PositiveInfinity))
                param += " -MinWidth " + control.MinWidth.ToString();

            if (!(double.IsNaN(control.Height)) && (control.Height != double.PositiveInfinity))
                param += " -Height " + control.Height.ToString();

            if (!(double.IsNaN(control.MaxHeight)) && (control.MaxHeight != double.PositiveInfinity))
                param += " -MaxHeight " + control.MaxHeight.ToString();

            if (!(double.IsNaN(control.MinHeight)) && (control.MinHeight != double.PositiveInfinity))
                param += " -MinHeight " + control.MinHeight.ToString();

            if (control.Parent is Grid)
            {
                param += " -Column " + Grid.GetColumn(control).ToString();
                param += " -Row " + Grid.GetRow(control).ToString();
                param += " -RowSpan " + Grid.GetRowSpan(control).ToString();
                param += " -ColumnSpan " + Grid.GetColumnSpan(control).ToString();
            }

            param += " -Margin " + control.Margin.ToString();



            param += " -VerticalAlignment " + control.VerticalAlignment.ToString();

            param += " -HorizontalAlignment " + control.HorizontalAlignment.ToString();



            return param;
        }


        /// <summary>
        /// Get -Children parameter filled with the content of the children passed
        /// </summary>
        /// <param name="controls"></param>
        /// <returns></returns>
        public static string GetChildrenParameter(List<ContentControl> controls)
        {
            string childrenscript = string.Empty;
            foreach (ContentControl control in controls)
            {
                if (control.Content != null)
                {
                    if (childrenscript != string.Empty)
                        childrenscript += ",";

                    childrenscript += "'" + control.Content.ToString() + "'";
                }
            }

            if (childrenscript != string.Empty)
                childrenscript = " -children " + childrenscript;


            return childrenscript;
        }


    }
}
